import {Router, Request, Response} from "express";
import {UsersModel} from "../models/usersModel";
import logger from "../logger";

require('express-async-errors');

export const user_router: Router = Router();

user_router.get('/selectOpenByIps', async (req: Request, res: Response) => {
    const ip = req.query.ip as string;
    const result = await UsersModel.find(
        {ips: ip},
        {openid: 1, _id: 0}
    );
    res.json(result);
});

user_router.get('/ips', async (req: Request, res: Response) => {
    const openid = req.query.openid as string;
    const result = await UsersModel.find({openid: openid}, {ips: 1, _id: 0});
    // logger.info({msg: 'result ', result});
    console.log(' result ', result[0]);
    if (result[0]) {
        res.json(result[0]);
    } else {
        res.json(['127.0.0.1']);
    }
});